import React, { ReactNode, createContext, useMemo, useState } from "react";

export type AuthProviderProps = {
  children: ReactNode;
};

export type AuthData = {
  username: string;
  password: string;
};

export type AuthMethod = {
  updateAuthData: (data: AuthData) => void;
};

export const AuthDataContext = createContext<AuthData>({} as AuthData);
export const AuthMethodContext = createContext<AuthMethod>({} as AuthMethod);

export default function AuthProvider({ children }: AuthProviderProps) {
  const [authData, setAuthData] = useState<AuthData>({} as AuthData);
  const authMethod = useMemo(
    () => ({
      updateAuthData: (data: AuthData) => {
        setAuthData(data);
      }
    }),
    []
  );
  return (
    <AuthDataContext.Provider value={authData}>
      <AuthMethodContext.Provider value={authMethod}>{children}</AuthMethodContext.Provider>
    </AuthDataContext.Provider>
  );
}
